var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    todayGoodsList:{
      type:Array,
      value:[],
      observer(newVal, oldVal, changedPath) {

      }
    },
    hotGoodsList:{
      type:Array,
      value:[],
      observer(newVal, oldVal, changedPath) {

      }
    },
    selecthdGoodsList:{
      type:Array,
      value:[],
      observer(newVal, oldVal, changedPath) {

      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    supplyChannel:2,//默认一件代发
    active: 0,
    selecthd:[]
  },
  ready(){
    this.data.active=app.cache['pages/home/home'].data.active;
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 广告选中那个
    selectAdvert(e){
      this.setData({
        active:e
      },()=>{
        app.cache['pages/home/home'].data.currPage=1;
        app.cache['pages/home/home'].data.selecthdGoodsList=[];
        app.cache['pages/home/home'].getshopProductsHdZq();
        setTimeout(()=>{
          app.cache['pages/home/home'].getShopProductsHdZqSales();
        },2000)
      })
      
    },
    getselecthd(){
      this.setData({
        selecthd:app.cache['pages/home/home'].data.selecthd
      })
    },
    // 加入购物车后修改数量
    updateCartNum(e){
      let total=e.detail.total;
      let index=e.detail.index;
      if(app.cache['pages/home/home'].data.active==0){
        this.setData({
          ["todayGoodsList["+index+"].cartNum"]:this.data.todayGoodsList[index].cartNum>0 ? this.data.todayGoodsList[index].cartNum+total : total
        })
      }else if(app.cache['pages/home/home'].data.active==1){
        this.setData({
          ["hotGoodsList["+index+"].cartNum"]:this.data.hotGoodsList[index].cartNum>0 ? this.data.hotGoodsList[index].cartNum+total : total
        })
      }else if(app.cache['pages/home/home'].data.active>2){
        this.setData({
          ["selecthdGoodsList["+index+"].cartNum"]:this.data.selecthdGoodsList[index].cartNum>0 ? this.data.selecthdGoodsList[index].cartNum+total : total
        })
      }
    },
    toxiaolu(event){
        wx.navigateTo({
          url: '/pages_subpack/xiaolu/xiaolu'
        })
    },
    // 选择切换
    onChange(event) {
      this.setData({
        active:event.detail.index
      })
      app.cache['pages/home/home'].data.active=event.detail.index;
      if(event.detail.index==0){
        // 加载今日新款
        // app.cache['pages/home/home'].getTodayProducts();
      }else if(event.detail.index==1){
        // 加载爆款
        // app.cache['pages/home/home'].getHotProducts();
      }
      // else if(event.detail.index==2){
      //   // 跳转到小鹿集市
      // }
      else if(event.detail.index >= 2){
        app.cache['pages/home/home'].data.currPage=1;
        app.cache['pages/home/home'].data.selecthdGoodsList=[];
        app.cache['pages/home/home'].getshopProductsHdZq();
        setTimeout(()=>{
          app.cache['pages/home/home'].getShopProductsHdZqSales();
        },2000)
      }
      // app.UpdateCart(event.detail.index);
      // console.info(app.cache_get(this).tabgoods.data);
    },

    // 有规格的商品调用规格
    onaddCart(e){
      let goods=e.detail.goods;
      let index=e.detail.index;
      let platform=e.detail.platform;
      if(platform=='self'){
        this.selectComponent("#suk").getProductsDetail(goods.id,index,this.data.supplyChannel);
      }
    },
    ajaxAddCart(index,goods,plus=true){
      let that=this;
      let userinfo = app.functions.getLocalStorage("user");
      let shop=app.functions.getLocalStorage("shop");
      if(app.functions.isEmpty(userinfo)){
        app.functions.islogin();
        return false;
      }
       let cart={
        supplyChannel:2,
        mid:userinfo.id,
        teamid:shop.id,
        productId:goods.id,
        total: plus ? 1 : -1,
        marketprice: goods.marketprice
      };
      //修改吉贡单规格商品
      if(goods.skus.length==1) {
        let skusOne = goods.skus[0];
        cart.skuId = skusOne.id;
        cart.skuTitle = skusOne.title;
        cart.marketprice = (user.isVip && skusOne.vipprice && goods.isvip) ? skusOne.vipprice : skusOne.marketprice;
        cart.vipprice = skusOne.vipprice;
      }

      if (goods.maxbuy && (goods.cartNum + cart.total) > goods.maxbuy) {
        app.functions.showToast("已经超过单次购买数量");
        return;
      }
      app.api.cartAdd(cart,(response)=>{
        if(response.code!=0){
          app.functions.showToast("加入到购物车失败");
          return ;
        }
        app.functions.showToast("已加入到购物车");
        app.cartNumber = app.cartNumber == 0 ? cart.total : parseInt(app.cartNumber) + cart.total;
        that.setData({
          ["items.cartNum"]:this.data.items.cartNum+cart.total
        })
        app.UpdateCart();
      },(error)=>{

      })
    },
    
  }
})
